package com.example.arve;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Build;

/**
 * Description:
 * Base activity is just the central hub of the ARVE. It doesn't exactly look amazing, but the current
 * interface is most definitely for prototype and testing purposes only.
 * 
 * What's working:
 *  - All button functionality works fine
 * What's not: 
 *  - Nothing!
 * @author Richard
 *
 */
public class BaseActivity extends ActionBarActivity 
{
	public static final String YOUTUBE_URL_MESSAGE = "com.example.arve.YOUTUBE_URL";
	
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);

        if (savedInstanceState == null) 
        {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {    
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.base, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    public void startYoutubeActivity(View _view)
    {
    	//Start the youtube activity
    	EditText youtube_url = (EditText)findViewById(R.id.youtube_url);
    	String extracted = youtube_url.getText().toString();
    	if(extracted.length() != 0)
    	{
    		//For now this just starts a web view and starts up the web page given by the text field
    		try
    		{
    			Intent youtube_intent = new Intent(this, YoutubeActivity.class);
    			youtube_intent.putExtra(YOUTUBE_URL_MESSAGE, extracted);
    			startActivity(youtube_intent);
    			//startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(extracted)));
    		}
    		catch(Exception _error)
    		{
    			//When we enter an invalid URL, it's considered an exception; this may be fixed if 
    			//we extend a web activity?
    			Toast.makeText(this, "Invalid URL: " + youtube_url, Toast.LENGTH_LONG).show();
    		}
    	}
    	else
    	{
    		//Just displays a message when we haven't entered a url
    		Toast.makeText(this, "Please enter a URL into the text box", Toast.LENGTH_LONG).show();
    	}
    }
    
    public void startPhotoActivity(View _view)
    {
    	try
    	{
    		//Setup the intent to start the camera activity
    		Intent camera_intent = new Intent(this, CameraActivity.class);
    		//And run it
    		startActivity(camera_intent);
    	}
    	catch(Exception _error)
    	{
    		
    	}
    }
    
    public void startQRCodeActivity(View _view)
    {
    	try
    	{
    		//Start our QRCode intent
    		Intent intent = new Intent(this, QRActivity.class);
    		startActivity(intent);
    	}
    	catch(Exception _error)
    	{
    		
    	}
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment 
    {

        public PlaceholderFragment() 
        {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_base, container, false);
            return rootView;
        }
    }

}
